<template>
  <div class="search-display-view">
    <div class="search-display-container">
      <div v-for="(novel, index) in novels" :key="index">
        <search-display-component :novel="novel" />
        <div class="search-divider">
          <el-divider class="search-divider-line" />
        </div>
      </div>
      <div class="search-pagination">
        <el-pagination
          background
          :page-count="pages"
          :current-page="page"
          :page-size="size"
          layout="prev, pager, next, jumper"
          @current-change="updateCurrentPage"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { searchBook } from "@/api";
import SearchDisplayComponent from "@/components/SearchDisplayComponent.vue";

export default {
  components: {
    SearchDisplayComponent,
  },
  data() {
    return {
      novels: [],
      page: 1,
      size: 20,
      total: 10,
      pages: 10,
    };
  },
  methods: {
    getSearchResult() {
      searchBook(this.$route.query.keyword, this.page, this.size).then(
        (res) => {
          if (res.data.success) {
            this.novels = res.data.result.records;
            this.pages = res.data.result.pages;
            this.total = res.data.result.total;
            this.$forceUpdate();
            //this.total = res.data.result.total;
          }
        }
      );
    },
    updateCurrentPage(page) {
      this.page = page;
      this.getSearchResult();
    },
  },
  computed: {
    searchKeyword() {
      return this.$route.query.keyword;
    },
  },
  watch: {
    $route(to, from) {
      if (to.query.keyword !== from.query.keyword) {
        this.getSearchResult();
      }
    },
  },
  created() {
    this.getSearchResult();
  },
};
</script>

<style>
.search-display-view {
  display: grid;
  background-color: rgb(239 239 239);
  justify-content: center;
}
.search-divider {
  position: relative;
  display: flex;
  justify-content: center;
  background-color: white;
  width: 1200px;
  margin: 0px;
  left: 150px;
}
.search-divider-line {
  margin: 0px 0px !important;
  width: 1100px !important;
}
.search-pagination {
  display: flex;
  justify-content: center;
  margin: 10px;
}
</style>